package com.videoaggre.zcl.ui.activity.agefans

import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.kingja.loadsir.core.LoadService
import com.videoaggre.zcl.R
import com.videoaggre.zcl.bean.AgefansCatalogType
import com.videoaggre.zcl.bean.AgefansCatalogTypeValue
import com.videoaggre.zcl.bean.AgefansItem
import com.videoaggre.zcl.common.dp
import com.videoaggre.zcl.common.showLoadingCallback
import com.videoaggre.zcl.databinding.AgefansItemCatalogBinding
import com.videoaggre.zcl.databinding.AgefansItemCatalogValueBinding
import com.videoaggre.zcl.helper.loadsir.LoadSirHelper
import com.videoaggre.zcl.helper.newInstance
import com.videoaggre.zcl.parse.BaseParse
import com.videoaggre.zcl.parse.impl.AgefansParse
import com.videoaggre.zcl.ui.adapter.AgefansSearchItemAdapter
import com.videoaggre.zcl.ui.adapter.BaseRecyclerAdapter
import com.videoaggre.zcl.ui.adapter.BaseSignCheckAdapter
import com.videoaggre.zcl.ui.adapter.BaseViewHolder
import com.videoaggre.zcl.ui.fragment.parseHtml
import com.videoaggre.zcl.ui.widget.CommonButton
import com.videoaggre.zcl.utils.CenterLayoutManager
import com.videoaggre.zcl.utils.FullyLinearLayoutManager
import kotlinx.android.synthetic.main.agefans_activity_catalog.*

class AgefansCatalogActivity : AgefansBaseRefreshActivity<AgefansItem>(R.layout.agefans_activity_catalog) {
    private val mAgefansSearchItemAdapter: AgefansSearchItemAdapter by newInstance()
    private val catalogs: MutableList<AgefansCatalogType> = AgefansParse.catalogs()
    override fun initView() {
        mBaseRecyclerAdapter = mAgefansSearchItemAdapter
        recyclerView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
        super.initView()
        rvCatalog.layoutManager = FullyLinearLayoutManager(this)
        rvCatalog.adapter = AgefansCatalogAdapter().also { it.datas = catalogs }
        refreshLayout.setEnableRefresh(false)
        LoadSirHelper.changePosition(mLoadService, 50.dp)
    }

    override fun createLoadService(): LoadService<*> = LoadSirHelper.registerScroll(recyclerView) { onReload() }

    override fun initData() {
        mLoadService.showLoadingCallback()
        onReload()
    }

    override fun onReload() {
        val queryKeys = mutableMapOf<String, String>()
        queryKeys["page"] = mAgefansPage.currentPage.toString()
        catalogs.forEach { queryKeys[it.queryKey] = it.getCheck() }
        parseHtml(BaseParse(this, this, AgefansParse.createCatalogLink(queryKeys), loadOrDialog = 1 == mAgefansPage.currentPage, loadService = mLoadService, refreshLayout = refreshLayout) {
            AgefansParse.parseSearch(it) {
                handResult(it)
            }
        })
    }

    inner class AgefansCatalogAdapter : BaseRecyclerAdapter<BaseViewHolder<AgefansItemCatalogBinding>, AgefansCatalogType>() {
        override fun onCreateViewHolder(parent: ViewGroup, p1: Int) = BaseViewHolder(AgefansItemCatalogBinding.inflate(getLayoutInflate(parent.context), parent, false))
        override fun onBindViewHolder(holder: BaseViewHolder<AgefansItemCatalogBinding>, bean: AgefansCatalogType, position: Int) {
            holder.mViewBinding.run {
                bean.run {
                    tvCatalog.text = name
                    rvCatalogValue.layoutManager = CenterLayoutManager(rvCatalogValue.context, LinearLayoutManager.HORIZONTAL, false)
                    rvCatalogValue.adapter = AgefansCatalogValueAdapter(rvCatalogValue, queryKey).also { it.datas = values }
                    (rvCatalogValue.itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false
                }
            }
        }
    }

    inner class AgefansCatalogValueAdapter(private val rvCatalogValue: RecyclerView, val queryKey: String) : BaseSignCheckAdapter<BaseViewHolder<AgefansItemCatalogValueBinding>, AgefansCatalogTypeValue>() {
        override fun onCreateViewHolder(parent: ViewGroup, p1: Int) = BaseViewHolder(AgefansItemCatalogValueBinding.inflate(getLayoutInflate(parent.context), parent, false))
        override fun onBindViewHolder(holder: BaseViewHolder<AgefansItemCatalogValueBinding>, bean: AgefansCatalogTypeValue, position: Int) {
            holder.mViewBinding.run {
                bean.run {
                    cbCatalogValue.text = typeName
                    cbCatalogValue.changeStyle(if (check) CommonButton.CB_STROKE_NORMAL else CommonButton.CB_STROKE_TEXT)
                    setViewClick(position, root)
                }
            }
        }

        override fun subOnClickAdapter(v: View, position: Int) {
            super.subOnClickAdapter(v, position)
            rvCatalogValue.layoutManager?.smoothScrollToPosition(rvCatalog, RecyclerView.State(), position)
            onRefresh(null)
        }
    }
}

